package tests; // not yet

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import sourceFiles.*;

import org.junit.Test;
import org.omg.PortableInterceptor.INACTIVE;

import Exceptions.*;
import Exceptions.CentralComputerExceptions.*;

/**
 * This test checks all the different methods in Application class
 * 
 * @author LASO-Team
 * 
 */
public class ApplicationTest {

	static private String masterXMLFilePath;
	static private String copyXMLFilePath;
	static private int numOfInitialRides;

	static private final int RANDOM_TEST_SIZE = 7;
	static private final int[] EDGE_OF_LEGAL_INT_VALUES = { 1, 2 };
	static private final int[] EDGE_OF_ILLEGAL_INT_VALUES = { -1, 0 };
	static private final int TEST_SIZE = RANDOM_TEST_SIZE
			+ EDGE_OF_LEGAL_INT_VALUES.length;
	private static final String IN_PROJECT_PATH_TO_TESTS = "\\src\\tests";
	static private final int MAX_RANDOM = 100;
	static private String[] legalRideNames;
	static private int[] legalMaxCapacities;
	static private int[] legalMinAges;
	static private int[] legalMinHeights;
	static private boolean[] isExtremeRides;
	static private int[] illegalMaxCapacities;
	static private int[] illegalMinAges;
	static private int[] illegalMinHeights;
	static private int[] illegalRideIDs;

	static private int[] nonExistingLegalRideIDs;

	static private int[] legalChildIDs;

	static private int[] legalGuardianIDs;

	static private int[] illegalChildIDs;
	static private int[] illegalGuardianIDs;
	static private int[] nonExistingLegalChildIDs;
	static private int[] nonExistingLegalGuardianIDs;

	static private int[] billingInfo;

	/**
	 * Initiating all the static fields for the tests with mostly random values
	 * used in the tests
	 * 
	 * @throws Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		assertTrue(EDGE_OF_LEGAL_INT_VALUES.length == EDGE_OF_ILLEGAL_INT_VALUES.length);
		String currentPath = new java.io.File(".").getCanonicalPath();
		currentPath = currentPath + IN_PROJECT_PATH_TO_TESTS;
		masterXMLFilePath = currentPath + "\\TestsMaster.xml";
		copyXMLFilePath = currentPath + "\\results\\ResultsUnitApplication.xml";

		LinkedList<Integer> legalMaxCapacitiesList = new LinkedList<Integer>();
		LinkedList<Integer> legalMinAgesList = new LinkedList<Integer>();
		LinkedList<Integer> legalMinHeightsList = new LinkedList<Integer>();

		LinkedList<Integer> illegalMaxCapacitiesList = new LinkedList<Integer>();
		LinkedList<Integer> illegalMinAgesList = new LinkedList<Integer>();
		LinkedList<Integer> illegalMinHeightsList = new LinkedList<Integer>();
		LinkedList<Integer> illegalRideIDsList = new LinkedList<Integer>();

		LinkedList<Integer> legalChildIDsList = new LinkedList<Integer>();
		LinkedList<Integer> illegalChildIDsList = new LinkedList<Integer>();

		LinkedList<Integer> legalGuardianIDsList = new LinkedList<Integer>();
		LinkedList<Integer> illegalGuardianIDsList = new LinkedList<Integer>();
		LinkedList<Integer> nonExistingLegalChildIDsList = new LinkedList<Integer>();
		LinkedList<Integer> nonExistingLegalGuardianIDsList = new LinkedList<Integer>();

		int numOfEdgeTests = EDGE_OF_LEGAL_INT_VALUES.length;
		for (int i = 0; i < numOfEdgeTests; i++) { // init the legal value first
			legalMaxCapacitiesList.add(EDGE_OF_LEGAL_INT_VALUES[i]);
			legalMinAgesList.add(EDGE_OF_LEGAL_INT_VALUES[i]);
			legalMinHeightsList.add(EDGE_OF_LEGAL_INT_VALUES[i]);
			illegalMaxCapacitiesList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);
			illegalMinAgesList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);
			illegalMinHeightsList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);
			illegalRideIDsList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);

			legalChildIDsList.add(EDGE_OF_LEGAL_INT_VALUES[i]);
			illegalChildIDsList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);

			legalGuardianIDsList.add(EDGE_OF_LEGAL_INT_VALUES[i]);
			illegalGuardianIDsList.add(EDGE_OF_ILLEGAL_INT_VALUES[i]);

		}

		Random rand = new Random();
		int newRandID;
		for (int i = 0; i < RANDOM_TEST_SIZE; i++) {
			// makes sure that the ID created is a unique ID
			newRandID = rand.nextInt(MAX_RANDOM) + 1;
			while (legalChildIDsList.contains(newRandID)) {
				newRandID = rand.nextInt(MAX_RANDOM) + 1;
			}
			legalChildIDsList.add(newRandID);
			legalGuardianIDsList.add(newRandID);

			legalMaxCapacitiesList.add(rand.nextInt(MAX_RANDOM) + 1);
			legalMinAgesList.add(rand.nextInt(MAX_RANDOM) + 1);
			legalMinHeightsList.add(rand.nextInt(MAX_RANDOM) + 1);

			illegalMaxCapacitiesList.add(-rand.nextInt(MAX_RANDOM));
			illegalMinAgesList.add(-rand.nextInt(Integer.MAX_VALUE));
			illegalMinHeightsList.add(-rand.nextInt(Integer.MAX_VALUE));
			illegalRideIDsList.add(-rand.nextInt(Integer.MAX_VALUE));

			illegalChildIDsList.add(-rand.nextInt(Integer.MAX_VALUE));

			illegalGuardianIDsList.add(-rand.nextInt(Integer.MAX_VALUE));

		}

		// add legal child and guardian IDs that will not be created
		for (int i = 0; i < TEST_SIZE; i++) {
			newRandID = rand.nextInt(MAX_RANDOM) + 1;
			while ((legalChildIDsList.contains(newRandID))
					|| (legalGuardianIDsList.contains(newRandID))) {
				newRandID = rand.nextInt(MAX_RANDOM) + 1;
			}
			nonExistingLegalChildIDsList.add(newRandID);
			nonExistingLegalGuardianIDsList.add(newRandID);

		}

		assertTrue(legalChildIDsList.size() == TEST_SIZE
				&& legalGuardianIDsList.size() == TEST_SIZE);

		legalRideNames = new String[TEST_SIZE];
		legalMaxCapacities = new int[TEST_SIZE];
		legalMinAges = new int[TEST_SIZE];
		legalMinHeights = new int[TEST_SIZE];
		isExtremeRides = new boolean[TEST_SIZE];
		illegalMaxCapacities = new int[TEST_SIZE];
		illegalMinAges = new int[TEST_SIZE];
		illegalMinHeights = new int[TEST_SIZE];
		illegalRideIDs = new int[TEST_SIZE];
		nonExistingLegalRideIDs = new int[TEST_SIZE];

		legalChildIDs = new int[TEST_SIZE];
		legalGuardianIDs = new int[TEST_SIZE];
		illegalChildIDs = new int[TEST_SIZE];
		illegalGuardianIDs = new int[TEST_SIZE];
		nonExistingLegalChildIDs = new int[TEST_SIZE];
		nonExistingLegalGuardianIDs = new int[TEST_SIZE];
		billingInfo = new int[TEST_SIZE];

		for (int i = 0; i < TEST_SIZE; i++) {
			legalRideNames[i] = UUID.randomUUID().toString();
			legalMaxCapacities[i] = legalMaxCapacitiesList.get(i);
			legalMinAges[i] = legalMinAgesList.get(i);
			legalMinHeights[i] = legalMinHeightsList.get(i);
			isExtremeRides[i] = rand.nextBoolean();

			illegalMaxCapacities[i] = illegalMaxCapacitiesList.get(i);
			illegalMinAges[i] = illegalMinAgesList.get(i);
			illegalMinHeights[i] = illegalMinHeightsList.get(i);
			illegalRideIDs[i] = illegalRideIDsList.get(i);

			// nonExistingLegalRideIDs[i] = nonExistinglegalRideIDsList.get(i);

			legalChildIDs[i] = legalChildIDsList.get(i);
			legalGuardianIDs[i] = legalGuardianIDsList.get(i);
			illegalChildIDs[i] = illegalChildIDsList.get(i);
			illegalGuardianIDs[i] = illegalGuardianIDsList.get(i);

			nonExistingLegalChildIDs[i] = nonExistingLegalChildIDsList.get(i);
			nonExistingLegalGuardianIDs[i] = nonExistingLegalGuardianIDsList
					.get(i);
			billingInfo[i] = rand.nextInt();
		}
		return;
	}

	/**
	 * Preparing a new XML file for the use of the current test 
	 * 
	 * @throws IOException
	 */
	@Before
	public void setUp() throws IOException {

		InputStream inStream = null;
		OutputStream outStream = null;

		File afile = new File(masterXMLFilePath);
		File bfile = new File(copyXMLFilePath);

		inStream = new FileInputStream(afile);
		outStream = new FileOutputStream(bfile);

		byte[] buffer = new byte[1024];

		int length;
		// copy the file content in bytes
		while ((length = inStream.read(buffer)) > 0) {

			outStream.write(buffer, 0, length);
		}
		inStream.close();
		outStream.close();
	}

	/**
	 * This method finds a new random ride ID which doesn't exist in the central computer. 
	 * 
	 * @param cc - an instance of CentralComputer.
	 * @param cc
	 * @return
	 */
	private int findNonExistindRandomRideID(CentralComputer cc) {
		Random rand = new Random();
		int newRandID = 0;
		boolean doesIDExists = true;
		while (doesIDExists) {
			newRandID = rand.nextInt(MAX_RANDOM) + 1;
			doesIDExists = false;
			LinkedList<IRide> currentRides = cc.getRidesList();
			for (int i = 0; i < currentRides.size(); i++) {
				if (currentRides.get(i).getRideID() == newRandID) {
					doesIDExists = true;
					break;
				}
			}
		}
		return newRandID;
	}

	/**
	 * This method tests the constructor of the application which uses an instance of central computer. 
	 * This method checks that we cannot create an application with null instead of a central computer.
	 * This method checks that we can create an application with a valid central computer.
	 *  
	 * @throws NullPointerException
	 * @throws XMLRidesParserException
	 */
	@Test
	public void testApplicationCtor() throws NullPointerException,
			XMLRidesParserException {
		// @SuppressWarnings("unused")

		ICentralComputer cc = new CentralComputer(copyXMLFilePath);

		Application app;
		try {
			app = new Application(null);
			fail("tried to create an Application with a null central computer");
		} catch (NullPointerException e) {
		}

		app = new Application(cc);

	}

	/**
	 * This method tests the process of logging in a guardian to the application and the central computer.
	 * This method checks that we cannot login a guardian with an illegal guardian ID number.
	 * This method checks that we cannot login a guardian using null instead of a password.
	 * This method checks that we cannot login a guardian with a guardian ID number which does not exist in the central computer.
	 * This method checks that we cannot login a guardian with a non-matching password.
	 * This method checks that we can login a guardian with legal parameters.
	 * 
	 * @throws XMLRidesParserException
	 * @throws InvalidGuardianID
	 * @throws GuardianAlreadyExist
	 * @throws InvalidPassword
	 * @throws GuardianIDDoesntExist
	 * @throws ErrorInGuardianPassword
	 */
	@Test
	public void testLogin() throws XMLRidesParserException, InvalidGuardianID,
			GuardianAlreadyExist, InvalidPassword, GuardianIDDoesntExist,
			ErrorInGuardianPassword {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);

		// creating a legal guardian.
		String password = cc.addGuardian(legalGuardianIDs[0], billingInfo[0]);

		// trying to connect a guardian with illegal guardian ID numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				cc.connectGuardian(illegalGuardianIDs[i], password);
				fail("Tried to connect the guardian with an illegal guardianID value of "
						+ illegalGuardianIDs[i]);
			} catch (InvalidGuardianID e) {
			}
		}

		// trying to connect a guardian with a null password.
		try {
			cc.connectGuardian(legalGuardianIDs[0], null);
			fail("Tried to connect the guardian with a null password ");
		} catch (InvalidPassword e) {
		}

		// trying to connect a non-existing guardian.
		try {
			cc.connectGuardian(nonExistingLegalGuardianIDs[0], password);
			fail("Tried to connect a non-existing guardian with guardianID value of "
					+ nonExistingLegalGuardianIDs[0]);
		} catch (GuardianIDDoesntExist e) {
		}

		// trying to connect a guardian with non-matching password.
		try {
			cc.connectGuardian(legalGuardianIDs[0], password + "aa");
			fail("Tried to connect a non-existing guardian with guardianID value of "
					+ nonExistingLegalGuardianIDs[0]);
		} catch (ErrorInGuardianPassword e) {
		}

		cc.connectGuardian(legalGuardianIDs[0], password);
		// guardian is connected to the application

		Application app = new Application(cc);

		app.login(legalGuardianIDs[0], cc.getGuardiansList().get(0)
				.getPassword());

		assertTrue(app.getGuardianID() == legalGuardianIDs[0]);

	}

	/**
	 * This method tests the process of logging out a guardian from the application and the central computer.
	 * 
	 * @throws XMLRidesParserException
	 */
	@Test
	public void testLogout() throws XMLRidesParserException {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);
		Application app = new Application(cc);

		app.logout();
		assertTrue(app.getGuardianID() == null);
	}

	/**
	 * This method tests the process of displaying the location of all the children of the guardian.
	 * This method checks that we cannot display children's location with an illegal guardian ID number.
	 * This method checks that we cannot display children's location with a guardian ID number which does not exist in the central computer.
	 * This method checks that we cannot display children's location with a guardian which does not have any children.
	 * This method checks that we can display children's location with legal parameters.
	 * 
	 * 
	 * @throws XMLRidesParserException
	 * @throws InvalidGuardianID
	 * @throws GuardianAlreadyExist
	 * @throws InvalidRideName
	 * @throws InvalidCapacity
	 * @throws InvalidAgeParam
	 * @throws InvalidHeightParam
	 * @throws GuardianIDDoesntExist
	 * @throws NoChildrenForGuardian
	 * @throws ChildAlreadyExist
	 * @throws InvalidChildID
	 * @throws InvalidRideId
	 * @throws ChildIDDoesntExist
	 * @throws RideIDDoesntExist
	 * @throws ChildAgeDoesntMatch
	 * @throws ChildHeightDoesntMatch
	 * @throws TicketDoesntExist
	 */
	@Test
	public void testDisplayChildrenPosition() throws XMLRidesParserException,
			InvalidGuardianID, GuardianAlreadyExist, InvalidRideName,
			InvalidCapacity, InvalidAgeParam, InvalidHeightParam,
			GuardianIDDoesntExist, NoChildrenForGuardian, ChildAlreadyExist,
			InvalidChildID, InvalidRideId, ChildIDDoesntExist,
			RideIDDoesntExist, ChildAgeDoesntMatch, ChildHeightDoesntMatch,
			TicketDoesntExist {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);

		// creating a guardian and a child for the test.
		cc.addGuardian(legalGuardianIDs[0], billingInfo[0]);

		// creating a ride for the test
		Integer createdRideID = cc.createRide(legalRideNames[0],
				isExtremeRides[0], legalMaxCapacities[0], legalMinAges[0],
				legalMinHeights[0]);

		// Trying to Retrieve Children Location with illegal guardianID
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				cc.retrieveChildrenLocation(illegalGuardianIDs[i]);
				fail("Tried to Retrieve Children Location with an illegal guardianID value of "
						+ illegalGuardianIDs[i]);
			} catch (InvalidGuardianID e) {
			}
		}

		// Trying to Retrieve Children Location for non-existing guardian ID
		// numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				cc.retrieveChildrenLocation(nonExistingLegalGuardianIDs[i]);
				fail("Tried to Retrieve Children Location with a non-existing guardian ID value of "
						+ nonExistingLegalGuardianIDs[i]);
			} catch (GuardianIDDoesntExist e) {
			}
		}

		// Trying to Retrieve Children Location for a guardian without children.
		assertTrue(cc.getGuardiansList().get(0).getChildrenList().size() == 0);
		try {
			cc.retrieveChildrenLocation(legalGuardianIDs[0]);
			fail("Tried to Retrieve Children Location for a guardian without children. guardian ID value of "
					+ legalGuardianIDs[0]);
		} catch (NoChildrenForGuardian e) {
		}

		// adding one child
		cc.addChildToGuardian(legalGuardianIDs[0], legalChildIDs[0],
				legalMinAges[0], legalMinHeights[0]);
		cc.buyingProcess(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);
		cc.validateChildForRide(createdRideID, legalGuardianIDs[0]);

		Map<Integer, Integer> childrensLocationMap;

		childrensLocationMap = cc.retrieveChildrenLocation(legalGuardianIDs[0]);

		assertTrue(childrensLocationMap.get(legalChildIDs[0]).equals(
				createdRideID));

		Application app = new Application(cc);

		Map<Integer, Integer> childrenPos = app
				.displayChildrenPosition(legalGuardianIDs[0]);

		assertTrue(childrenPos.get(legalChildIDs[0]).equals(createdRideID));

	}

	/**
	 * This method tests the process of displaying child's E-ticket.
	 * This method checks that we cannot display child's E-ticket with an illegal child ID number.
	 * This method checks that we cannot display child's E-ticket with a child ID number which does not exist in the central computer.
	 * This method checks that we can display child's E-ticket with legal parameters.
	 * 
	 * @throws XMLRidesParserException
	 * @throws InvalidGuardianID
	 * @throws GuardianAlreadyExist
	 * @throws GuardianIDDoesntExist
	 * @throws InvalidHeightParam
	 * @throws InvalidAgeParam
	 * @throws ChildAlreadyExist
	 * @throws InvalidChildID
	 * @throws InvalidRideName
	 * @throws InvalidCapacity
	 * @throws InvalidRideId
	 * @throws ChildIDDoesntExist
	 * @throws RideIDDoesntExist
	 */
	@Test
	public void testDisplayChildETicket() throws XMLRidesParserException,
			InvalidGuardianID, GuardianAlreadyExist, GuardianIDDoesntExist,
			InvalidHeightParam, InvalidAgeParam, ChildAlreadyExist,
			InvalidChildID, InvalidRideName, InvalidCapacity, InvalidRideId,
			ChildIDDoesntExist, RideIDDoesntExist {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);

		// creating a guardian and a child for the test.
		cc.addGuardian(legalGuardianIDs[0], billingInfo[0]);
		cc.addChildToGuardian(legalGuardianIDs[0], legalChildIDs[0],
				legalMinAges[0], legalMinHeights[0]);
		// creating a ride for the test
		Integer createdRideID = cc.createRide(legalRideNames[0],
				isExtremeRides[0], legalMaxCapacities[0], legalMinAges[0],
				legalMinHeights[0]);

		cc.buyingProcess(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);
		cc.buyingProcess(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);
		cc.buyingProcess(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);

		// retrieving an E-Ticket for a child with illegal ID numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				cc.retrieveChildETicket(illegalChildIDs[i]);
				fail("Tried to retrieve an E-Ticket for a child with an illegal childID value of "
						+ illegalChildIDs[i]);
			} catch (InvalidChildID e) {
			}
		}

		// retrieving an E-Ticket for non-existing child ID numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				cc.retrieveChildETicket(nonExistingLegalChildIDs[i]);
				fail("Tried to retrieve an E-Ticket for non-existing child ID  value of "
						+ nonExistingLegalChildIDs[i]);
			} catch (ChildIDDoesntExist e) {
			}
		}

		Application app = new Application(cc);
		ETicket eTicket = app.displayChildETicket(legalChildIDs[0]);
		
		int reg = eTicket.getRegualrRidesTickets();
		int extreme = 0;
		if (eTicket.getExtremeRidesList().size() != 0){
			extreme = eTicket.getExtremeRidesList().get(createdRideID);
		}
		assertTrue((reg + extreme) == 3);

	}

	/**
	 * This method tests the process of displaying the catalog of all the rides.
	 * 
	 * @throws XMLRidesParserException
	 * @throws InvalidRideName
	 * @throws InvalidCapacity
	 * @throws InvalidAgeParam
	 * @throws InvalidHeightParam
	 */
	@Test
	public void testDisplayRidesCatalog() throws XMLRidesParserException,
			InvalidRideName, InvalidCapacity, InvalidAgeParam,
			InvalidHeightParam {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);
		Application app = new Application(cc);

		numOfInitialRides = cc.getRidesList().size();

		// creating rides for the test
		for (int i = 0; i < TEST_SIZE; i++) {
			cc.createRide(legalRideNames[i], isExtremeRides[i],
					legalMaxCapacities[i], legalMinAges[i], legalMinHeights[i]);
		}

		LinkedList<RideEntry> tempRideCatalog = app.displayRidesCatalog();
		int i = cc.getRidesCatalog().size();

		assertTrue(tempRideCatalog.size() == TEST_SIZE + numOfInitialRides);

	}

	/**
	 * This method tests the process of buying a ticket.
	 * This method checks that we cannot buy a ticket with an illegal rideID number.
	 * This method checks that we cannot buy a ticket with an illegal guardian ID number.
	 * This method checks that we cannot buy a ticket with a guardian ID number which does not exist in the central computer.
	 * This method checks that we cannot buy a ticket with a child ID number which does not exist in the central computer.
	 * This method checks that we can buy a ticket with legal parameters.
	 * 
	 * 
	 * @throws XMLRidesParserException
	 * @throws InvalidGuardianID
	 * @throws GuardianAlreadyExist
	 * @throws GuardianIDDoesntExist
	 * @throws InvalidHeightParam
	 * @throws InvalidAgeParam
	 * @throws ChildAlreadyExist
	 * @throws InvalidChildID
	 * @throws InvalidRideName
	 * @throws InvalidCapacity
	 * @throws ChildIDDoesntExist
	 * @throws RideIDDoesntExist
	 * @throws InvalidRideId
	 */
	@Test
	public void testBuyTicket() throws XMLRidesParserException,
			InvalidGuardianID, GuardianAlreadyExist, GuardianIDDoesntExist,
			InvalidHeightParam, InvalidAgeParam, ChildAlreadyExist,
			InvalidChildID, InvalidRideName, InvalidCapacity,
			ChildIDDoesntExist, RideIDDoesntExist, InvalidRideId {
		CentralComputer cc = new CentralComputer(copyXMLFilePath);

		// creating a guardian and a child for the test.
		cc.addGuardian(legalGuardianIDs[0], billingInfo[0]);
		cc.addChildToGuardian(legalGuardianIDs[0], legalChildIDs[0],
				legalMinAges[0], legalMinHeights[0]);
		// creating a ride for the test
		Integer createdRideID = cc.createRide(legalRideNames[0],
				isExtremeRides[0], legalMaxCapacities[0], legalMinAges[0],
				legalMinHeights[0]);

		Application app = new Application(cc);
		// performing a buy with illegal rideID
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				app.buyTicket(illegalRideIDs[i], legalGuardianIDs[0],
						legalChildIDs[0]);
				fail("Tried to performing a buy with an illegal RideID value of "
						+ illegalRideIDs[i]);
			} catch (InvalidRideId e) {
			}
		}

		int newRandID = findNonExistindRandomRideID(cc);

		try {
			app.buyTicket(newRandID, legalGuardianIDs[0], legalChildIDs[0]);
			fail("Tried to performing a buy with an non-existing Ride value of "
					+ newRandID);
		} catch (RideIDDoesntExist e) {
		}

		// performing a buy with illegal guardianID
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				app.buyTicket(createdRideID, illegalGuardianIDs[i],
						legalChildIDs[0]);
				fail("Tried to performing a buy with an illegal guardianID value of "
						+ illegalRideIDs[i]);
			} catch (InvalidGuardianID e) {
			}
		}

		// performing a buy with non-existing guardian ID numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				app.buyTicket(createdRideID, nonExistingLegalGuardianIDs[i],
						legalChildIDs[0]);
				fail("Tried to performing a buy with a non-existing guardian ID  value of "
						+ nonExistingLegalGuardianIDs[i]);
			} catch (GuardianIDDoesntExist e) {
			}
		}

		// performing a buy with non-existing child ID numbers.
		for (int i = 0; i < TEST_SIZE; i++) {
			try {
				app.buyTicket(createdRideID, legalGuardianIDs[0],
						nonExistingLegalChildIDs[i]);
				fail("Tried to performing a buy with a non-existing child ID  value of "
						+ nonExistingLegalChildIDs[i]);
			} catch (ChildIDDoesntExist e) {
			}
		}

		assertTrue(!cc.getTicketsSold().containsKey(createdRideID));
		app.buyTicket(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);
		assertTrue(cc.getTicketsSold().get(createdRideID) == 1);

		ChildEntry child = cc.getChildrenList().get(0);
		assertTrue(child.getChildsTicket().getExtremeRidesList().size()
				+ child.getChildsTicket().getRegualrRidesTickets() == 1);
	}

	 /**
	 * This method tests the process of canceling a ticket.
	 * This method checks that we cannot cancel a ticket with an illegal rideID number.
	 * This method checks that we cannot cancel a ticket with a rideID which does not exist in the central computer.
	 * This method checks that we cannot cancel a ticket with an illegal guardian ID number.
	 * This method checks that we cannot cancel a ticket with a guardian ID number which does not exist in the central computer.
	 * This method checks that we cannot cancel a ticket with an illegal child ID number.
	 * This method checks that we cannot cancel a ticket with a child ID number which does not exist in the central computer.
	 * This method checks that we can cancel a ticket with legal parameters.
	 * 
	 * @throws XMLRidesParserException
	 * @throws GuardianIDDoesntExist
	 * @throws InvalidHeightParam
	 * @throws InvalidAgeParam
	 * @throws ChildAlreadyExist
	 * @throws GuardianAlreadyExist
	 * @throws InvalidRideName
	 * @throws InvalidCapacity
	 * @throws ChildIDDoesntExist
	 * @throws RideIDDoesntExist
	 * @throws TicketDoesntExist
	 * @throws InvalidRideId
	 * @throws InvalidChildID
	 * @throws InvalidGuardianID
	 */
	@Test
	 public void testCancelTicket() throws XMLRidesParserException,
		GuardianIDDoesntExist, InvalidHeightParam, InvalidAgeParam,
		ChildAlreadyExist, GuardianAlreadyExist, InvalidRideName,
		InvalidCapacity, ChildIDDoesntExist, RideIDDoesntExist,
		TicketDoesntExist, InvalidRideId, InvalidChildID, InvalidGuardianID {

	CentralComputer cc = new CentralComputer(copyXMLFilePath);

	// creating a guardian and a child for the test.
	cc.addGuardian(legalGuardianIDs[0], billingInfo[0]);
	cc.addChildToGuardian(legalGuardianIDs[0], legalChildIDs[0],
			legalMinAges[0], legalMinHeights[0]);
	// creating a ride for the test
	Integer createdRideID = cc.createRide(legalRideNames[0],
			isExtremeRides[0], legalMaxCapacities[0], legalMinAges[0],
			legalMinHeights[0]);

	cc.buyingProcess(createdRideID, legalGuardianIDs[0], legalChildIDs[0]);

	Application app = new Application(cc);
	// canceling a buy with illegal rideID
	for (int i = 0; i < TEST_SIZE; i++) {
		try {
			//cc.cancelAcquisition(illegalRideIDs[i], legalGuardianIDs[0],
			app.cancelTicket(illegalRideIDs[i], legalGuardianIDs[0],
					legalChildIDs[0]);
			fail("Tried to performing a buy with an illegal RideID value of "
					+ illegalRideIDs[i]);
		} catch (InvalidRideId e) {
		}
	}

	int newRandID = findNonExistindRandomRideID(cc);

	try {
		app.cancelTicket(newRandID, legalGuardianIDs[0],
				legalChildIDs[0]);
		fail("Tried to performing a buy with an non-existing Ride value of "
				+ nonExistingLegalRideIDs[0]);
	} catch (RideIDDoesntExist e) {
	}

	// performing a buy with illegal guardianID
	for (int i = 0; i < TEST_SIZE; i++) {
		try {
			app.cancelTicket(createdRideID, illegalGuardianIDs[i],
					legalChildIDs[0]);
			fail("Tried to performing a buy with an illegal guardianID value of "
					+ illegalGuardianIDs[i]);
		} catch (InvalidGuardianID e) {
		}
	}

	// performing a buy with non-existing guardian ID numbers.
	for (int i = 0; i < TEST_SIZE; i++) {
		try {
			app.cancelTicket(createdRideID,
					nonExistingLegalGuardianIDs[i], legalChildIDs[0]);
			fail("Tried to performing a buy with a non-existing guardian ID  value of "
					+ nonExistingLegalGuardianIDs[i]);
		} catch (GuardianIDDoesntExist e) {
		}
	}

	// performing a buy with illegal childID
	for (int i = 0; i < TEST_SIZE; i++) {
		try {
			app.cancelTicket(createdRideID, legalGuardianIDs[0],
					illegalChildIDs[i]);
			fail("Tried to performing a buy with an illegal childID value of "
					+ illegalChildIDs[i]);
		} catch (InvalidChildID e) {
		}
	}

	// performing a buy with non-existing child ID numbers.
	for (int i = 0; i < TEST_SIZE; i++) {
		try {
			app.cancelTicket(createdRideID, legalGuardianIDs[0],
					nonExistingLegalChildIDs[i]);
			fail("Tried to performing a buy with a non-existing child ID  value of "
					+ nonExistingLegalChildIDs[i]);
		} catch (ChildIDDoesntExist e) {
		}
	}

	assertTrue(cc.getTicketsSold().get(createdRideID) == 1);
	app.cancelTicket(createdRideID, legalGuardianIDs[0],
			legalChildIDs[0]);
	assertTrue(!cc.getTicketsSold().containsKey(createdRideID));

	ChildEntry child = cc.getChildrenList().get(0);
	assertTrue(child.getChildsTicket().getExtremeRidesList().size()
			+ child.getChildsTicket().getRegualrRidesTickets() == 0);
		 
	 }


}
